package com.thh.create;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thh.dao.BookDaoImpl;
import com.thh.pojo.Book;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.util.List;

/**
 * 演示ES的Document的创建
 */
public class App15_CreateClusterDocument {

    //定义客户端
    TransportClient client;

    private void createMapping() throws Exception{
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject()
                    .startObject("book")
                    .startObject("properties")
                    .startObject("price").field("type","long").endObject()
                    .startObject("bookdesc").field("type","text").field("analyzer","ik_max_word").endObject()
                    .startObject("id").field("type","long").field("store",true).endObject()
                    .startObject("pic").field("type","text").field("store",true).field("index",false).endObject()
                    .startObject("bookname").field("type","text").field("analyzer","ik_max_word").field("store",true).endObject()
                    .endObject()
                    .endObject()
                    .endObject();

            //删除索引
            client.admin().indices()
                    .preparePutMapping("xiaoyaya_index") //在那个索引中创建映射
                    .setType("book") //设置类型
                    .setSource(builder) //传入映射json对象
                    .get() //执行操作
            ;
    }

    /**
     * 批量
     * @throws Exception
     */
    @Test
    public void test4() throws Exception  {


        //先添加映射：第一次的时候
        createMapping();


        //导入数据
        List<Book> allBooks = new BookDaoImpl().findAllBooks();
        for (Book book : allBooks) {
            //把对象转成json字符串
            ObjectMapper om = new ObjectMapper();
            String bookStr = om.writeValueAsString(book);

            //创建文档
            client.prepareIndex("xiaoyaya_index","book",book.getId()+"")
                    .setSource(bookStr, XContentType.JSON)//添加文档
                    .get();//执行创建
        }
    }


    //初始化
    @Before
    public void init() throws Exception {
        //1、创建一个Settings对象，相当于一个配置信息。主要配置集群的名称。 cluster.name配置集群名称
        Settings settings = Settings.builder().put("cluster.name","leyou-es-cluster").build();
        //2、创建一个客户端client对象。
        client = new PreBuiltTransportClient(settings);
        //3、使用client对象创建一个索引库。
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9301));
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9302));
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9303));
    }

    //释放资源
    @After
    public void close(){
        //4、关闭client对象。
        client.close();
    }
}
